﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN08_2E : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");  
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.AddClientScript();
            this.ViewMenu();           
            this.SetErrorMessage();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnCONFIRM_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            SaveData(true);
        }
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN08_1S.aspx");
    }
    #endregion
    #region "---------- Methods -----------"    
    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
    }
    private void BindData(int ID)
    {
        List<GSBBondInterest> gSBBondInterest = new List<GSBBondInterest>();
        DataTable dt = new SPR_GSBBondInterest().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, gSBBondInterest);

            txtGSBBondCode.Text = gSBBondInterest[0].GSBBondCode;
            txtInterestPaymentDate.Text = gSBBondInterest[0].InterestPaymentDate.DateToString();
            txtInterestRate.Text = gSBBondInterest[0].InterestRate.ToString();           
            txtStatus.Text = gSBBondInterest[0].IsConfirmed == false ? "สร้างรายการ" : "ยืนยัน";              

            hidGSBBondInterestID.Value = gSBBondInterest[0].GSBBondInterestID.ToString();
            hidUpdateDate.Value = gSBBondInterest[0].UpdatedDate.ToString();          
        }
    }   
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqInterestPaymentDate.SetRequiredField(lblInterestPaymentDate, txtInterestPaymentDate, btnSAVE);
        reqGSBBondCode.SetRequiredField(lblGSBBondCode, txtGSBBondCode, btnSAVE);       
        reqInterestRate.SetRequiredField(lblInterestRate, txtInterestRate, btnSAVE);
    }
    private void SaveData(bool IsConfirmed)
    {
        try
        {
            GSBBondInterest gSBBondInterest = new GSBBondInterest();

            gSBBondInterest.GSBBondInterestID = hidGSBBondInterestID.Value.AsInt32();
            gSBBondInterest.GSBBondCode = txtGSBBondCode.Text;
            gSBBondInterest.InterestPaymentDate = txtInterestPaymentDate.Text.StringToDateAllowNull();      
            gSBBondInterest.InterestRate = txtInterestRate.Text.AsDecimal().Value;
            gSBBondInterest.IsConfirmed = IsConfirmed;
            gSBBondInterest.SetStandardField();

            int iID = new SPR_GSBBondInterest().UPDATE(gSBBondInterest);

            if (iID > 0)
            {
                if (IsConfirmed)
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN08_3V.aspx?&id={0}", gSBBondInterest.GSBBondInterestID)));
                }
                else
                {
                    //this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN08_2E.aspx?&id={0}", hidGSBBondInterestID.Value.AsInt32())));
                    this.JS_Alert(Resources.ifrsResource.SaveData);
                    this.BindData(gSBBondInterest.GSBBondInterestID);
                }
            }
            else
            {
                if (iID == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN08_2E;
    }
    #endregion
}